﻿#include<iostream>
using namespace std;

template<class K>
struct BSTNode
{

	BSTNode(const K& key)
		:_key(key)
		,_left(nullptr)
		,_right(nullptr)
	{}

	K _key;
	BSTNode* _left;
	BSTNode* _right;
};

//Binary Search Tree
template<class K>
class BSTree
{
public:
	using Node = BSTNode<K>;

	BSTree() = default;

	BSTree(const BSTree& t)
	{
		_root = copy(t._root);
	}

	~BSTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

	bool insert(const K& k)
	{
		if (_root == nullptr)
		{
			_root = new Node(k);
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (k < cur->_key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (k > cur->_key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		Node* new_node = new Node(k);
		if (k < parent->_key)
		{
			parent->_left = new_node;
		}
		else
		{
			parent->_right = new_node;
		}
		return true;
	}

	bool find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key < cur->_key)
			{
				cur = cur->_left;
			}
			else if(key > cur->_key)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	bool earse(const K& key)
	{
		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (key < cur->_key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (key < cur->_key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				// 0-1个孩⼦的情况
				// 删除情况1 2 3均可以直接删除，改变⽗亲对应孩⼦指针指向即可
				if (cur->_left == nullptr)
				{
					if (parent == nullptr)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_right;
						else
							parent->_right = cur->_right;
					}
					delete cur;
					return true;
				}
				else if (cur->_right == nullptr)
				{
					if (parent == nullptr)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;
					}
					delete cur;
					return true;
				}
				//有两个
				else
				{
					Node* rightMinP = cur;
					Node* rightMin = cur->_right;
					while (rightMin -> _left)
					{
						rightMinP = rightMin;
						rightMin = rightMin->_left;
					}
					
					cur->_key = rightMin->_key;

					if (rightMinP->_left == rightMin)
						rightMin->_left = rightMinP->_left;
					else
						rightMinP->_left = rightMin->_right;


					delete rightMin;
					return true;
				}
			}
		}

		return false;
	}

private:
	Node* copy(Node* node)
	{
		if (node)
		{
			return nullptr;
		}

		Node* new_node = new Node(node->_key);
		new_node->_left = copy(node->_left);
		new_node->_right = copy(node->_right);

		return new_node;
	}

	void Destroy(Node* node)
	{
		if (node == nullptr)
			return;

		Destroy(node->_left);
		Destroy(node->_right);
		delete node;
	}


private:
	Node* _root = nullptr;
};

int main()
{
	BSTree<int> b;
	b.insert(1);
	b.insert(2);
	b.insert(3);
	b.earse(1);
	
	return 0;
}